package kamikaze.service.admin;

import javax.persistence.EntityManager;
import javax.servlet.http.HttpServletRequest;

import kamikaze.entity.User;
import kamikaze.support.HttpMethod;
import kamikaze.support.MessageView;
import kamikaze.support.Service;
import kamikaze.support.ServiceConfig;
import kamikaze.support.ServiceContext;

@ServiceConfig(path = "admin/ChangeUserStatus", method = HttpMethod.POST)
public class AdminChangeUserStatusService implements Service {

	@Override
	public String serve(ServiceContext ctx) throws Exception {
		ctx.getUserSession().ensureLoginAsAdmin();

		HttpServletRequest req = ctx.getRequest();
		final boolean suspend = "true".equals(req.getParameter("suspend"));
		final String userId = req.getParameter("userId");
		EntityManager em = ctx.getEntityManager();
		User user = em.find(User.class, Long.valueOf(userId));
		user.setSuspended(suspend);
		em.persist(user);

		return MessageView.from(ctx)
				.setMessage("user status updated")
				.setNextURL("/service/admin/list_user").getViewName();

	}

}
